<?php

namespace App\Http\Controllers\Admin;

use App\Services\WxMass\WxMassService;
use App\Services\Wxtj\WxtjArticle;
use App\Services\Wxtj\WxtjUser;
use Illuminate\Http\Request;

/**
 * 微信群发统计
 * @package App\Http\Controllers\Admin
 */
class WxtjArticleController extends BaseWxController
{
    /**
     * 图文群发每日数据
     * @param Request $request
     */
    public function summary(Request $request, WxtjArticle $wxtjArticle)
    {
        if ($request->ajax()) {
            //关注渠道
            $query = \App\Models\WxtjArticleSummary::select('*');
            //查询指定微信应用
            $curWxMp = \Auth::user()->getWxMpCurrent();
            $appid = $curWxMp ? $curWxMp['appid'] : '';
            $query->where('appid', $appid);
            //消息ID，由msg_data_id加_消息次序索引
            $msgid = $request->input('msgid', 0);
            if ($msgid) {
                $query->where('msgid', $msgid);
            }
            //开始结束时间
            $startTime = $request->get('start_time', '');
            $startTime = $startTime ? date('Y-m-d', strtotime($startTime)) : date('Y-m-d', strtotime('-7 days'));
            $endTime = $request->get('end_time', '');
            $endTime = $endTime ? date('Y-m-d', strtotime($endTime)) : date('Y-m-d', strtotime('-1 days'));
            if (!empty($startTime) && !empty($endTime)) {
                $query->whereBetween('ref_date', [$startTime, $endTime]);
            } elseif (!empty($startTime) && empty($endTime)) {
                $query->whereRaw("ref_date >= '" . $startTime . "'");
            } elseif (empty($startTime) && !empty($endTime)) {
                $query->whereRaw("ref_date <= '" . $endTime . "'");
            }
            $rows = $query->get();
            $rows = $rows ? $rows->toArray() : [];
            $formatData = formatChartDataByDate(
                $rows,
                'ref_date',
                ['int_page_read_user', 'int_page_read_count', 'ori_page_read_user', 'ori_page_read_count','share_user','share_count','add_to_fav_user','add_to_fav_count'],
                $startTime, $endTime);
            return $formatData;
        }
        $this->viewData['startDate'] = date('Y-m-d', strtotime('-7 days'));
        $this->viewData['endDate'] = date('Y-m-d', strtotime('-1 days'));
        return view('admin.wxtjarticle.summary', $this->viewData);
    }

    /**
     * 图文群发总数据
     * @param Request $request
     * @param WxtjArticle $wxtjArticle
     * @return array|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @throws \Exception
     */
    public function total(Request $request, WxtjArticle $wxtjArticle)
    {
        if ($request->ajax()) {
            //关注渠道
            $query = \App\Models\WxtjArticleTotal::select('*');
            //查询指定微信应用
            $curWxMp = \Auth::user()->getWxMpCurrent();
            $appid = $curWxMp ? $curWxMp['appid'] : '';
            $query->where('appid', $appid);
            //消息ID，由msg_data_id加_消息次序索引
            $msgid = $request->input('msgid', 0);
            if ($msgid) {
                $query->where('msgid', $msgid);
            }
            //开始结束时间
            $startTime = $request->get('start_time', '');
            $startTime = $startTime ? date('Y-m-d', strtotime($startTime)) : date('Y-m-d', strtotime('-7 days'));
            $endTime = $request->get('end_time', '');
            $endTime = $endTime ? date('Y-m-d', strtotime($endTime)) : date('Y-m-d', strtotime('-1 days'));
            if (!empty($startTime) && !empty($endTime)) {
                $query->whereBetween('ref_date', [$startTime, $endTime]);
            } elseif (!empty($startTime) && empty($endTime)) {
                $query->whereRaw("ref_date >= '" . $startTime . "'");
            } elseif (empty($startTime) && !empty($endTime)) {
                $query->whereRaw("ref_date <= '" . $endTime . "'");
            }
            $rows = $query->get();
            $rows = $rows ? $rows->toArray() : [];
            $formatData = formatChartDataByDate(
                $rows,
                'ref_date',
                [
                    'target_user','int_page_read_user', 'int_page_read_count', 'ori_page_read_user', 'ori_page_read_count','share_user','share_count','add_to_fav_user','add_to_fav_count',
                    'int_page_from_session_read_user','int_page_from_session_read_count','int_page_from_hist_msg_read_user','int_page_from_hist_msg_read_count',
                    'int_page_from_feed_read_user','int_page_from_feed_read_count','int_page_from_friends_read_user','int_page_from_friends_read_count',
                    'int_page_from_other_read_user','int_page_from_other_read_count','int_page_from_kanyikan_read_user','int_page_from_kanyikan_read_count',
                    'int_page_from_souyisou_read_user','int_page_from_souyisou_read_count','feed_share_from_session_user','feed_share_from_session_cnt',
                    'feed_share_from_feed_user','feed_share_from_feed_cnt','feed_share_from_other_user','feed_share_from_other_cnt',
                ],
                $startTime, $endTime);
            return $formatData;
        }
        $this->viewData['startDate'] = date('Y-m-d', strtotime('-7 days'));
        $this->viewData['endDate'] = date('Y-m-d', strtotime('-1 days'));
        return view('admin.wxtjarticle.total', $this->viewData);
    }

    /**
     * 图文阅读统计数据
     * @param Request $request
     * @param WxtjArticle $wxtjArticle
     * @return array|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @throws \Exception
     */
    public function userread(Request $request, WxtjArticle $wxtjArticle)
    {
        if ($request->ajax()) {
            //关注渠道
            $query = \App\Models\WxtjArticleUserread::select('*');
            //查询指定微信应用
            $curWxMp = \Auth::user()->getWxMpCurrent();
            $appid = $curWxMp ? $curWxMp['appid'] : '';
            $query->where('appid', $appid);
            //开始结束时间
            $startTime = $request->get('start_time', '');
            $startTime = $startTime ? date('Y-m-d', strtotime($startTime)) : date('Y-m-d', strtotime('-7 days'));
            $endTime = $request->get('end_time', '');
            $endTime = $endTime ? date('Y-m-d', strtotime($endTime)) : date('Y-m-d', strtotime('-1 days'));
            if (!empty($startTime) && !empty($endTime)) {
                $query->whereBetween('ref_date', [$startTime, $endTime]);
            } elseif (!empty($startTime) && empty($endTime)) {
                $query->whereRaw("ref_date >= '" . $startTime . "'");
            } elseif (empty($startTime) && !empty($endTime)) {
                $query->whereRaw("ref_date <= '" . $endTime . "'");
            }
            $rows = $query->get();
            $rows = $rows ? $rows->toArray() : [];
            $formatData = formatChartDataByDate(
                $rows,
                'ref_date',
                ['int_page_read_user', 'int_page_read_count', 'ori_page_read_user', 'ori_page_read_count','share_user','share_count','add_to_fav_user','add_to_fav_count'],
                $startTime, $endTime);
            return $formatData;
        }
        $this->viewData['startDate'] = date('Y-m-d', strtotime('-7 days'));
        $this->viewData['endDate'] = date('Y-m-d', strtotime('-1 days'));
        return view('admin.wxtjarticle.userread', $this->viewData);
    }

    /**
     * 图文统计分时数据
     * @param Request $request
     * @param WxtjArticle $wxtjArticle
     * @return array|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @throws \Exception
     */
    public function userreadhour(Request $request, WxtjArticle $wxtjArticle)
    {
        if ($request->ajax()) {
            //关注渠道
            $query = \App\Models\WxtjArticleUserreadhour::select('*');
            //查询指定微信应用
            $curWxMp = \Auth::user()->getWxMpCurrent();
            $appid = $curWxMp ? $curWxMp['appid'] : '';
            $query->where('appid', $appid);
            //开始结束时间
            $startTime = $request->get('start_time', '');
            $startTime = $startTime ? date('Y-m-d', strtotime($startTime)) : date('Y-m-d', strtotime('-7 days'));
            $query->where('ref_date', $startTime);
            //用户来源
            $userSource = $request->input('user_source', 0);
            $query->where('user_source', $userSource);

            $rows = $query->get();
            $rows = $rows ? $rows->toArray() : [];
            $formatData = formatChartDataByHour(
                $rows,
                'ref_hour',
                ['int_page_read_user', 'int_page_read_count', 'ori_page_read_user', 'ori_page_read_count','share_user','share_count','add_to_fav_user','add_to_fav_count']
            );
            return $formatData;
        }
        $this->viewData['userSourceTypeList'] = $wxtjArticle->getUserSourceList();
        $this->viewData['startDate'] = date('Y-m-d', strtotime('-1 days'));
        return view('admin.wxtjarticle.userreadhour', $this->viewData);
    }

    /**
     * 图文分享转发数据(天)
     * @param Request $request
     * @param WxtjArticle $wxtjArticle
     * @return array|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @throws \Exception
     */
    public function usershare(Request $request, WxtjArticle $wxtjArticle)
    {
        if ($request->ajax()) {
            //关注渠道
            $query = \App\Models\WxtjArticleUsershare::select('*');
            //查询指定微信应用
            $curWxMp = \Auth::user()->getWxMpCurrent();
            $appid = $curWxMp ? $curWxMp['appid'] : '';
            $query->where('appid', $appid);
            //开始结束时间
            $startTime = $request->get('start_time', '');
            $startTime = $startTime ? date('Y-m-d', strtotime($startTime)) : date('Y-m-d', strtotime('-7 days'));
            $endTime = $request->get('end_time', '');
            $endTime = $endTime ? date('Y-m-d', strtotime($endTime)) : date('Y-m-d', strtotime('-1 days'));
            if (!empty($startTime) && !empty($endTime)) {
                $query->whereBetween('ref_date', [$startTime, $endTime]);
            } elseif (!empty($startTime) && empty($endTime)) {
                $query->whereRaw("ref_date >= '" . $startTime . "'");
            } elseif (empty($startTime) && !empty($endTime)) {
                $query->whereRaw("ref_date <= '" . $endTime . "'");
            }
            //分享场景
            $shareScene = $request->input('share_scene', 0);
            if ($shareScene){
                $query->where('share_scene', $shareScene);
            }

            $rows = $query->get();
            $rows = $rows ? $rows->toArray() : [];
            $formatData = formatChartDataByDate(
                $rows,
                'ref_date',
                ['share_count', 'share_user'],
                $startTime, $endTime);
            return $formatData;
        }
        $this->viewData['shareSceneList'] = $wxtjArticle->getShareSceneList();
        $this->viewData['startDate'] = date('Y-m-d', strtotime('-7 days'));
        $this->viewData['endDate'] = date('Y-m-d', strtotime('-1 days'));
        return view('admin.wxtjarticle.usershare', $this->viewData);
    }

    /**
     * 图文分享转发数据(时)
     * @param Request $request
     * @param WxtjArticle $wxtjArticle
     * @return array|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @throws \Exception
     */
    public function usersharehour(Request $request, WxtjArticle $wxtjArticle)
    {
        if ($request->ajax()) {
            //关注渠道
            $query = \App\Models\WxtjArticleUsersharehour::select('*');
            //查询指定微信应用
            $curWxMp = \Auth::user()->getWxMpCurrent();
            $appid = $curWxMp ? $curWxMp['appid'] : '';
            $query->where('appid', $appid);
            //开始结束时间
            $startTime = $request->get('start_time', '');
            $startTime = $startTime ? date('Y-m-d', strtotime($startTime)) : date('Y-m-d', strtotime('-7 days'));
            $query->where('ref_date', $startTime);
            //分享场景
            $shareScene = $request->input('share_scene', 0);
            if ($shareScene){
                $query->where('share_scene', $shareScene);
            }

            $rows = $query->get();
            $rows = $rows ? $rows->toArray() : [];
            $formatData = formatChartDataByHour(
                $rows,
                'ref_hour',
                ['share_count', 'share_user']);
            return $formatData;
        }
        $this->viewData['shareSceneList'] = $wxtjArticle->getShareSceneList();
        $this->viewData['startDate'] = date('Y-m-d', strtotime('-1 days'));
        return view('admin.wxtjarticle.usersharehour', $this->viewData);
    }
}
